﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Entities;
using STT.Business;
using STT.Layer.Connection;
using System.Data;
namespace STT.Admin.UserControls
{
    public partial class UcCongViec : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                LoadddlKhoaHoc();
                DDLGiangVien();
            }
            LoadDSCV();
        }
        private void DDLGiangVien()
        {
            Methods.GiangVienSelectAllForCV(ref DdlGiangVien);
        }
        protected void btTaocv_Click(object sender, EventArgs e)
        {
            if (DdlLoaiCongViec.SelectedValue.ToString() == "1")
            {
                if (BTbCongViec.SelectByMHMaGVMaLop(DdlGiangVien.SelectedValue.ToString(), ddlMonHoc.SelectedValue.ToString(), ddlChonLop.SelectedValue.ToString(),"1").Rows.Count > 0)
                {
                    ThongBao("Môn học " + " " + ddlMonHoc.SelectedItem.ToString() + " " + "của lớp " + " " + ddlChonLop.SelectedItem.ToString() + " đã được phân công coi thi");
                }
                else
                {
                    DataTable dt = tb.GetDataTableById2("TbKhoaHoc_SelectByID", ddlChonKhoa.SelectedValue.ToString());
                    BTbCongViec.InsertCongViecCoiThi(
                         new ETbCongViec
                         {
                             NDCongViec = "Coi thi môn "+ddlMonHoc.SelectedItem.ToString() 
                             +" của lớp "+ ddlChonLop.SelectedItem.ToString()+" vào "+ ddlCaThi.SelectedItem.ToString() +
                             " ngày " + txtNgayThi.Text,
                             MaGV = DdlGiangVien.SelectedValue.ToString(),
                             MaLop = int.Parse(ddlChonLop.SelectedValue.ToString()),
                             MaMonHoc = int.Parse(ddlMonHoc.SelectedValue.ToString()),
                             ThoiGianBatDau = Convert.ToDateTime(dt.Rows[0]["ThoiGianBatDau"].ToString()),
                             ThoiGianKetThuc = Convert.ToDateTime(dt.Rows[0]["ThoiGianKetThuc"].ToString()),
                             TrangThai = 0,
                             NgayCoiThi = Convert.ToDateTime(txtNgayThi.Text),
                             CaThi = int.Parse(ddlCaThi.SelectedValue.ToString()),
                             PhongThi = txtPhongThi.Text,
                             LoaiCongViec=1
                         }
                        );
                    ThongBao("Đã tạo thành công..!");
                    Huy();
                    LoadddlKhoaHoc();
                    LoadDSCV();
                }
            }
            else if (DdlLoaiCongViec.SelectedValue.ToString() == "2")
            {
                if (BTbCongViec.SelectByMHMaGVMaLop(DdlGiangVien.SelectedValue.ToString(), ddlMonHoc.SelectedValue.ToString(), ddlChonLop.SelectedValue.ToString(), "2").Rows.Count > 0)
                {
                    ThongBao("Môn học " + " " + ddlMonHoc.SelectedItem.ToString() + " " + "của lớp" + " " + ddlChonLop.SelectedItem.ToString() + "đã được phân công chấm thi");
                }
                else
                {
                    DataTable dt = tb.GetDataTableById2("TbKhoaHoc_SelectByID", ddlChonKhoa.SelectedValue.ToString());
                    BTbCongViec.InsertCongViecChamThi(
                         new ETbCongViec
                         {
                             NDCongViec = "Chấm thi môn " + ddlMonHoc.SelectedItem.ToString()
                             + " của lớp " + ddlChonLop.SelectedItem.ToString(),
                             MaGV = DdlGiangVien.SelectedValue.ToString(),
                             MaLop = int.Parse(ddlChonLop.SelectedValue.ToString()),
                             MaMonHoc = int.Parse(ddlMonHoc.SelectedValue.ToString()),
                             ThoiGianBatDau = Convert.ToDateTime(dt.Rows[0]["ThoiGianBatDau"].ToString()),
                             ThoiGianKetThuc = Convert.ToDateTime(dt.Rows[0]["ThoiGianKetThuc"].ToString()),
                             TrangThai = 0,
                             NgayNhanBaiThi = Convert.ToDateTime(txtNgayNhanBT.Text),
                             NgayNopKQChamThi = Convert.ToDateTime(Convert.ToDateTime(txtNgayNhanBT.Text).AddDays(10)),
                             LoaiCongViec = 2
                         }
                        );
                    ThongBao("Đã tạo thành công..!");
                    Huy();
                    LoadddlKhoaHoc();
                    LoadDSCV();
                }
            }
            else if (DdlLoaiCongViec.SelectedValue.ToString() == "3")
            {
                if (BTbCongViec.SelectByMHMaGVMaLop(DdlGiangVien.SelectedValue.ToString(), ddlMonHoc.SelectedValue.ToString(), ddlChonLop.SelectedValue.ToString(), "3").Rows.Count > 0)
                {
                    ThongBao("Môn học " + " " + ddlMonHoc.SelectedItem.ToString() + " " + "của lớp" + " " + ddlChonLop.SelectedItem.ToString() + "đã được phân công ra đề");
                }
                else
                {
                    DataTable dt = tb.GetDataTableById2("TbKhoaHoc_SelectByID", ddlChonKhoa.SelectedValue.ToString());
                    BTbCongViec.InsertCongViecRaDe(
                         new ETbCongViec
                         {
                             NDCongViec = "Ra đề thi môn " + ddlMonHoc.SelectedItem.ToString()
                            + " của lớp " + ddlChonLop.SelectedItem.ToString(),
                             MaGV = DdlGiangVien.SelectedValue.ToString(),
                             MaLop = int.Parse(ddlChonLop.SelectedValue.ToString()),
                             MaMonHoc = int.Parse(ddlMonHoc.SelectedValue.ToString()),
                             ThoiGianBatDau = Convert.ToDateTime(dt.Rows[0]["ThoiGianBatDau"].ToString()),
                             ThoiGianKetThuc = Convert.ToDateTime(dt.Rows[0]["ThoiGianKetThuc"].ToString()),
                             TrangThai = 0,
                             NgayRaDe = Convert.ToDateTime(txtngayrade.Text),
                             LoaiCongViec=3
                         }
                        );
                    ThongBao("Đã tạo thành công..!");
                    Huy();
                    LoadddlKhoaHoc();
                    LoadDSCV();
                }
            }
        }
        private void LoadDSCV()
        {
            DataTable dt = tb.GetDataTableById2("TbCongViec_SelectByLoaiCV","1");//1 coi thi ,2 chấm thi,3 ra đề
            gvCoiThi.DataSource = dt;
            gvCoiThi.DataBind();
            DataTable dt2 = tb.GetDataTableById2("TbCongViec_SelectByLoaiCV", "2");//1 coi thi ,2 chấm thi,3 ra đề
            GvChamThi.DataSource = dt2;
            GvChamThi.DataBind();
            DataTable dt3 = tb.GetDataTableById2("TbCongViec_SelectByLoaiCV", "3");//1 coi thi ,2 chấm thi,3 ra đề
            GvRaDe.DataSource = dt3;
            GvRaDe.DataBind();
        }
        private void LoadddlKhoaHoc()
        {
            Methods.KhoaHoc(ref ddlChonKhoa);
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void ddlChonKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlChonKhoa.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn chưa chọn khóa học...!");
                }
                else if (DdlGiangVien.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn chưa nhập mã giảng viên...!");
                    LoadddlKhoaHoc();
                }
                else
                {
                    Methods.LopHocByKhoaHocID(ref ddlChonLop, ddlChonKhoa.SelectedValue.ToString(), DdlGiangVien.SelectedValue.ToString());
                }
            }
            catch { }
        }
        private void Huy()
        {
            //txtMaGiangVien.Text = ""; 
            txtNgayThi.Text = "";
            //txtNoiDungCV.Text = "";
            txtPhongThi.Text = "";
            txtNgayNhanBT.Text = "";
            txtngayrade.Text = "";
            //txtNoiDungCV.Text = "";  
            ddlChonLop.Items.Clear();
            ddlMonHoc.Items.Clear();
            
        }
        protected void ddlChonLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlChonLop.SelectedItem.ToString() == "---------Chọn---------")
                {
                    ThongBao("Bạn hãy chọn lớp học...!");

                }
                else
                {
                    Methods.MonHocByMaLopTKBNamHoc(ref ddlMonHoc, ddlChonLop.SelectedValue.ToString());
                }
            }
            catch (Exception ex)
            {
                Response.Write("");
            }
        }

        protected void gvCongViec_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvCongViec_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvCongViec_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
        }
        protected void DdlLoaiCongViec_SelectedIndexChanged(object sender, EventArgs e)
        {
            Huy();

            if (DdlLoaiCongViec.SelectedValue.ToString() == "1")
            {
                        
                pnCoiThi.Visible = true;
                pnChamThi.Visible = false;
                pnRaDe.Visible = false;
            }
            else if (DdlLoaiCongViec.SelectedValue.ToString() == "2")
            {
                pnCoiThi.Visible = false;
                pnChamThi.Visible = true;
                pnRaDe.Visible = false;
            }
            else if (DdlLoaiCongViec.SelectedValue.ToString() == "3") {
                pnCoiThi.Visible = false;
                pnChamThi.Visible = false;
                pnRaDe.Visible = true;
            }
        }

        protected void DdlGiangVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DdlGiangVien.SelectedItem.ToString() == "---------Chọn---------")
            {
                ThongBao("Bạn hãy chọn mã giảng viên...!");

            }
            else
            {
                txtMaGiangVien.Text = DdlGiangVien.SelectedValue.ToString();
                ddlChonLop.Items.Clear();
                ddlMonHoc.Items.Clear();
            }
        }

        protected void GvRaDe_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GvRaDe_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GvRaDe_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void GvChamThi_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GvChamThi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GvChamThi_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void btnxoa_Click(object sender, EventArgs e)
        {

        }
    }
}